package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.dao.PictureDao;
import com.itheima.dao.PictureSortDao;
import com.github.pagehelper.Page;
import com.itheima.pojo.Picture;
import com.itheima.pojo.PictureSort;
import com.itheima.service.PictureSortService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.IdWorker;
import com.itheima.vo.PictureSortVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class PictureSortServieImpl implements PictureSortService {
    @Autowired
    private PictureSortDao pictureSortDao;
    @Autowired
    private PictureDao pictureDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * 添加图片分类
     *
     * @param pictureSortVO
     * @return
     */
    @Override
    public void add(PictureSortVO pictureSortVO) {
        PictureSort pictureSort = new PictureSort();
        pictureSort.setUid(idWorker.nextId() + "");
        pictureSort.setFileUid(pictureSortVO.getFileUid());
        pictureSort.setName(pictureSortVO.getName());
        pictureSort.setStatus(pictureSortVO.getStatus());
        pictureSort.setCreateTime(DateUtils.getNowTime());
        pictureSort.setUpdateTime(DateUtils.getNowTime());
        pictureSort.setParentUid(pictureSortVO.getParentUid());
        pictureSort.setSort(pictureSortVO.getSort());
        pictureSort.setIsShow(pictureSortVO.getIsShow());
        pictureSortDao.insert(pictureSort);
    }

    /**
     * 删除图片分类
     *
     * @param pictureSortVO
     * @return
     */
    @Override
    public void delete(PictureSortVO pictureSortVO) {
        Picture picture = new Picture();
        picture.setPictureSortUid(pictureSortVO.getUid());
        //判断分类是否包含图片
        System.out.println(pictureSortVO.getUid());
         List<Picture> pictures = pictureDao.select(picture);
        if (pictures.size() == 0) {
            //包含图片
            throw new RuntimeException("图片分类中包含图片,不可以直接删除");
        }
        PictureSort pictureSort = new PictureSort();
        pictureSort.setUid(pictureSortVO.getUid());
        //删除做逻辑删除,设置状态为0
        pictureSortVO.setStatus(0);
        //修改状态就相当于删除
        edit(pictureSortVO);
    }

    /**
     * 编辑图片分类
     *
     * @param pictureSortVO
     * @return
     */
    @Override
    public void edit(PictureSortVO pictureSortVO) {
        PictureSort pictureSort = new PictureSort();
        pictureSort.setUid(pictureSortVO.getUid());
        pictureSort.setFileUid(pictureSortVO.getFileUid());
        pictureSort.setName(pictureSortVO.getName());
        pictureSort.setStatus(pictureSortVO.getStatus());
        pictureSort.setUpdateTime(DateUtils.getNowTime());
        pictureSort.setParentUid(pictureSortVO.getParentUid());
        pictureSort.setSort(pictureSortVO.getSort());
        pictureSort.setIsShow(pictureSortVO.getIsShow());
        pictureSortDao.updateByPrimaryKeySelective(pictureSort);
    }

    /**
     * 获取图片分类列表
     *
     * @param pictureSortVO
     * @return
     */
    @Override
    public Page<PictureSort> getList(PictureSortVO pictureSortVO) {
        //设置默认当前页
        if (pictureSortVO.getCurrentPage() == null || "".equals(pictureSortVO.getCurrentPage())) {
            pictureSortVO.setCurrentPage(0);
        }
        //设置默认每页条数
        if (pictureSortVO.getPageSize() == null || "".equals(pictureSortVO.getPageSize())) {
            pictureSortVO.setPageSize(10);
        }
        //当前页
        Integer currentPage = pictureSortVO.getCurrentPage();
        //每页数据
        Integer pageSize = pictureSortVO.getPageSize();
        //分页查询
        PageHelper.startPage(currentPage, pageSize);

        //构造条件查询
        Example example = new Example(PictureSort.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", 1);
        if (pictureSortVO.getName() != null && "".equals(pictureSortVO.getName())) {
            criteria.andLike("name", "%" + pictureSortVO.getName() + "%");
        }
        example.orderBy("sort").desc();
        return (Page<PictureSort>) pictureSortDao.selectByExample(example);
    }

    /**
     * 通过Uid获取图片分类列表
     *
     * @param pictureSortVO
     * @return
     */
    @Override
    public PictureSort getPictureSortByUid(PictureSortVO pictureSortVO) {
        if (pictureSortVO.getStatus() == 0) {
            throw new RuntimeException("图片状态为已删除");
        }
        return pictureSortDao.selectByPrimaryKey(pictureSortVO.getUid());
    }

    /**
     * 图片分类置顶
     *
     * @param pictureSortVO
     * @return
     */
    @Override
    public Page<PictureSort> stick(PictureSortVO pictureSortVO) {
        PictureSort pictureSort = new PictureSort();
        pictureSort.setUid(pictureSortVO.getUid());
        //设置权重101
        pictureSort.setSort(101);
        //更新时间
        pictureSort.setUpdateTime(DateUtils.getNowTime());
        pictureSortDao.updateByPrimaryKeySelective(pictureSort);
        return (Page<PictureSort>) getList(pictureSortVO);
    }
}
